{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Answers to Assignment 3 - Python Fundamentals I\n",
    "\n",
    "Complete the tasks below. You should turn in a single Jupyter notebook named `3_first_last.ipynb` (substitute your first and last name); please run Kernel > Restart & Run All on your notebook before turning in. Also turn in the input CSV file used in question B3. For questions where Python scripts are used instead of the Jupyter notebook (questions A1, B1, B2), instructions are provided about pasting output/scripts into Markdown cells in your notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to A. Python basics, strings, printing (Shaw Exercises 1–10)\n",
    "\n",
    "A1. Take the file of Python code you generated in Exercise 1, called `ex1.py`, and run it on the command line. Copy the command and output from your terminal and paste it in a markdown cell in your Jupyter notebook. (Ex1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Content of `ex1.py`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# ex1.py\n",
    "print(\"Hello World!\")\n",
    "print(\"Hello Again\")\n",
    "print(\"I like typing this.\")\n",
    "print(\"This is fun.\")\n",
    "print('Yay! Printing.')\n",
    "print(\"I'd much rather you 'not'.\")\n",
    "print('I \"said\" do not touch this.')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "$ python ex1.py\n",
    "Hello World!\n",
    "Hello Again\n",
    "I like typing this.\n",
    "This is fun.\n",
    "Yay! Printing.\n",
    "I'd much rather you 'not'.\n",
    "I \"said\" do not touch this.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the symbols \\`\\`\\` around a block of text render that text as code not plaintext. A single newline between rows of plaintext must be separated with a `<BR>` to be rendered on separate lines."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A2. Print a sentence that contains a phrase in double quotes; print a sentence that contains an apostrophe. (Ex1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Kuiper belt's over 100,000 \"KBOs\" (Kuiper belt objects) are found 30 to 50 AUs from the Sun.\n"
     ]
    }
   ],
   "source": [
    "# print the sentence with a comma separating it into two strings for readability\n",
    "print('The Kuiper belt\\'s over 100,000 \"KBOs\" (Kuiper belt objects)', \n",
    "      'are found 30 to 50 AUs from the Sun.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A3. Deliberately enter five incorrect commands (in separate cells) and interpret the error messages. (Ex1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"No parentheses\")? (<ipython-input-2-6f8f9919fdaf>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-2-6f8f9919fdaf>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    print \"No parentheses\"\u001b[0m\n\u001b[0m                         ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"No parentheses\")?\n"
     ]
    }
   ],
   "source": [
    "# print something -- which version of python is this?\n",
    "print \"No parentheses\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Missing parentheses in call to 'print'. Python 3 requires parentheses around those objects to be printed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-3-df30210d2def>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-3-df30210d2def>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    print('It's my birthday')\u001b[0m\n\u001b[0m              ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# a sentence with an apostrophe\n",
    "print('It's my birthday')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Invalid syntax. The single quote `'` either needs to be wrapped in double quotes `\"` or escaped with `\\`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-4-5f1956aaaff5>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-4-5f1956aaaff5>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    x++\u001b[0m\n\u001b[0m       ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# let's try to increment x by 1\n",
    "x = 5\n",
    "x++"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Invalid syntax. Python does not support the `x++` increment operator or its variations. You can use `x += 1` or `x = x + 1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-5-85a1509462b6>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-5-85a1509462b6>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    x+1 for x in range(3)\u001b[0m\n\u001b[0m          ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# list comprehension\n",
    "x+1 for x in range(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Invalid syntax. The list comprehension must be wrapped in square brackets like so: `[x+1 for x in range(3)]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-43e8814c49dd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# trying in vain to assign to a tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'b'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'd'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# trying in vain to assign to a tuple\n",
    "t = ('a', 'b', 'c')\n",
    "t[3] = 'd'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just what it says: 'tuple' object does not support item assignment. If you need to change a tuple, you should probably just use a list instead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A4. Add comments to your code in \\#2 and \\#3 explaining what is happening. (Ex2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer: see above"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A5. Write and evaluate five mathematical expressions. (Ex3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2/5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.142857142857143"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*11/7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Optional: Use the many features of the `math` module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mathematical constant $\\pi$ differs from the quotient 22/7, an approximation of $\\pi$, by only 0.04%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0004024994347707008"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(22/7 - math.pi) / math.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A6. Assign values to three numeric and three string variables. (Ex4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "num1 = 7\n",
    "num2 = 11\n",
    "num3 = num1 + num2\n",
    "str1 = 'first'\n",
    "str2 = 'second'\n",
    "str3 = 'third'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A7. Print values of the six variables you assigned in \\#6. (Ex4–5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum of the first number (7) and the second number (11) is the third (18).\n"
     ]
    }
   ],
   "source": [
    "# note we can hit enter after the open-parentheses\n",
    "print('The sum of the %s number (%s) and the %s number (%s) is the %s (%s).' % (\n",
    "    str1, num1, str2, num2, str3, num3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A8. Print two strings in raw format (%r formatter) and two strings in general/string format (%s formatter) with formatting characters. (Ch6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'hello'\t'A\\tB'\n",
      "hello\tA\tB\n"
     ]
    }
   ],
   "source": [
    "# notice how the tab characters are rendered\n",
    "print('%r\\t%r\\n%s\\t%s' % ('hello', 'A\\tB', 'hello', 'A\\tB'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A9. Concatenate two strings into a third string, then find the length of all three strings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = 'Bird is '\n",
    "s2 = 'the word'\n",
    "s3 = s1 + s2\n",
    "len(s3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A10. Print the three strings from \\#9 with a tab between the first and second and a newline between the second and third."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bird is \tthe word\n",
      "Bird is the word\n"
     ]
    }
   ],
   "source": [
    "print('%s\\t%s\\n%s' % (s1, s2, s3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A11. Print the three strings from \\#9 with a stored formatter. (Ch7–8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bird is \tthe word\n",
      "Bird is the word\n"
     ]
    }
   ],
   "source": [
    "fmtr = '%s\\t%s\\n%s'\n",
    "print(fmtr % (s1, s2, s3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A12. Print a piece of text with five lines using both newline characters and a text block. (Ex9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A mathematician, prophetic,\n",
      "invented a language, herpetic.\n",
      "decidedly brilliant,\n",
      "syntacticly elegant,\n",
      "Made ideas far less hypothetic.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('A mathematician, prophetic,\\n'\n",
    "      'invented a language, herpetic.\\n'\n",
    "      'decidedly brilliant,\\n'\n",
    "      'syntacticly elegant,\\n'\n",
    "      'Made ideas far less hypothetic.\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A mathematician, prophetic,\n",
      "invented a language, herpetic.\n",
      "decidedly brilliant,\n",
      "syntacticly elegant,\n",
      "Made ideas far less hypothetic.\n"
     ]
    }
   ],
   "source": [
    "print('''A mathematician, prophetic,\n",
    "invented a language, herpetic.\n",
    "decidedly brilliant,\n",
    "syntacticly elegant,\n",
    "Made ideas far less hypothetic.''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A13. Print a string containing a backslash, single-quote, double-qoute, newline, and tab. (Ex10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a backslash \\, single-quote ', double-qoute \", newline,\n",
      "and a\ttab.\n"
     ]
    }
   ],
   "source": [
    "print('This is a backslash \\\\, single-quote \\', double-qoute \", newline,\\nand a\\ttab.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to B. Taking input, reading and writing files, functions (Shaw Exercises 11-26) \n",
    "\n",
    "B1. Write some code, without using functions, that calculates the average of 5 numbers. Do it three different ways:\n",
    "\n",
    "- Write a .py file that takes input from the command line using `input()`. After the script works, paste the text of the file into your Jupyter notebook.\n",
    "- Write a .py file that takes input from the command line using `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
    "- Enter code into two Jupyter notebooks cells: the first stores value as variables, and the second computes the average."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B1-1:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# assign3_qB1-1.py\n",
    "print('Hello! I am Average-Bot! Let me average five numbers please!')\n",
    "n1 = float(input('Number 1? '))\n",
    "n2 = float(input('Number 2? '))\n",
    "n3 = float(input('Number 3? '))\n",
    "n4 = float(input('Number 4? '))\n",
    "n5 = float(input('Number 5? '))\n",
    "print('Shazzam! The average is %s!' % ((n1+n2+n3+n4+n5)/5))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice we have to convert the entered numbers (strings) to floats."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B1-2:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# assign_qB1-2.py\n",
    "from sys import argv\n",
    "\n",
    "x = len(argv)\n",
    "if x != 6:\n",
    "    print('Error! Only %s arguments!\\n,'\n",
    "          'Average-Bot requires five numbers as arguments please!' % x)\n",
    "else:\n",
    "    n1 = float(argv[1])\n",
    "    n2 = float(argv[2])\n",
    "    n3 = float(argv[3])\n",
    "    n4 = float(argv[4])\n",
    "    n5 = float(argv[5])\n",
    "    print('Shazzam! The average is %s!' % ((n1+n2+n3+n4+n5)/5))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, notice we have to convert the entered numbers (strings) to floats. What would happen if we converted them to ints instead?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B1-3:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5]\n",
    "average = sum(numbers)/5\n",
    "average"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's a lot easier to get input from the user when you are the user!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B2. Using functions, write some code that takes two strings, prints them with the first letter capitalized, prints them with all letters capitalized, prints the first and last letter of each, prints the length of each, and then prints the concatenation of the two strings. Do it two different ways:\n",
    "\n",
    "- Write a .py file that uses `argv`. After the script works, paste the text of the file into your Jupyter notebook.\n",
    "- In your Jupyter notebook, comment out the `argv` portions and hard code in the values of your strings. Then make sure the code runs the same."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B2-1:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# assign3_qB2.py\n",
    "from sys import argv\n",
    "\n",
    "s1 = argv[1] # 'apple'\n",
    "s2 = argv[2] # 'banana'\n",
    "\n",
    "def exerciseB2(string1, string2):\n",
    "    print(string1.capitalize(), string2.capitalize())\n",
    "    print(string1.upper(), string2.upper())\n",
    "    print(string1[0], string1[-1], string2[0], string2[-1])\n",
    "    print(len(string1), len(string2))\n",
    "    print(len(string1)+len(string2))\n",
    "\n",
    "exerciseB2(s1, s2)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B2-2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Apple Banana\n",
      "APPLE BANANA\n",
      "a e b a\n",
      "5 6\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "# from sys import argv\n",
    "\n",
    "s1 = 'apple' # argv[1]\n",
    "s2 = 'banana' # argv[2]\n",
    "\n",
    "def exerciseB2(string1, string2):\n",
    "    print(string1.capitalize(), string2.capitalize())\n",
    "    print(string1.upper(), string2.upper())\n",
    "    print(string1[0], string1[-1], string2[0], string2[-1])\n",
    "    print(len(string1), len(string2))\n",
    "    print(len(string1)+len(string2))\n",
    "\n",
    "exerciseB2(s1, s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B3. Using a text editor, create a comma-separated values (CSV) file with 5 columns and 5 rows. Save it in the same directory as your Jupyter notebook. In the Jupyter notebook, read and print the file in different ways, and write new files, as follows:\n",
    "\n",
    "- Read your CSV file using `read()`, `readline()`, or `readlines()`, and print the output to the screen (`print()` command is optional in notebooks!).\n",
    "- Do the same but use a `with` block and a different one of `read()`, `readline()`, or `readlines()`.\n",
    "- Using either of the two above methods, then change one row of data, and write your data to a new CSV file.\n",
    "- Read your CSV file using Pandas and display the resulting DataFrame.\n",
    "- Save your DataFrame to a new file using Pandas."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B3-1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index,col1,col2,col3,col4\n",
      "row1,1,2,3,4\n",
      "row2,5,6,7,8\n",
      "row3,9,10,11,12\n",
      "row4,13,14,15,16\n",
      "\n"
     ]
    }
   ],
   "source": [
    "txt = open('../../data/assign3_qB3.csv')\n",
    "print(txt.read())\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B3-2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index,col1,col2,col3,col4\n",
      "row1,1,2,3,4\n",
      "row2,5,6,7,8\n",
      "row3,9,10,11,12\n",
      "row4,13,14,15,16\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('../../data/assign3_qB3.csv', 'r') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B3-3:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../../data/assign3_qB3.csv', 'r') as f:\n",
    "    data = f.readlines()\n",
    "    data[2] = 'row2,17,18,19,20\\n'\n",
    "with open('assign3_qB3a.csv', 'w') as target:\n",
    "    for row in data:\n",
    "        target.write(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['index,col1,col2,col3,col4\\n',\n",
       " 'row1,1,2,3,4\\n',\n",
       " 'row2,17,18,19,20\\n',\n",
       " 'row3,9,10,11,12\\n',\n",
       " 'row4,13,14,15,16\\n']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B3-4:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>row1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>row2</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>row3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>row4</th>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       col1  col2  col3  col4\n",
       "index                        \n",
       "row1      1     2     3     4\n",
       "row2      5     6     7     8\n",
       "row3      9    10    11    12\n",
       "row4     13    14    15    16"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('../../data/assign3_qB3.csv', index_col=0)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer to B3-5:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('assign3_qB3b.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
